<?php
/**
 * 部门信息控制器
 * @author YangL
 **/
namespace Admin\Controller;
use Common\Controller\WahahabaseController;
class DepartmentController extends WahahabaseController{

	protected $department_model,$role_model;//,$departmentPermission_model;

	public function _initialize() {
		parent::_initialize();
		$this->department_model = D("Common/Department");
		$this->role_model = D("Common/Role");
		//$this->departmentPermission_model = D("Common/DepartmentPermission");
	}

	public function index(){
		/**搜索条件**/
		$where = array("d.is_deleted"=>array('neq', 1));
		$deptname = I('request.dept_name');
		$deptcode = trim(I('request.dept_code'));
		if($deptname){
			$where['d.department_name'] = array('like',"%$deptname%");
		}
		
		//总条数
		$count=$this->department_model->alias("d")->where($where)->count();
		$page = $this->page($count, 20);
		
        $depts = $this->department_model->alias("d")
			->field('d.id,department_name,department_code,d.add_time,d.last_time,r.`name`,u.real_name')
			->join(array(
				' LEFT JOIN '.C('DB_PREFIX').'role r ON d.department_role = r.id',
				' LEFT JOIN '.C('DB_PREFIX').'users u ON d.add_user = u.id',
			))
            ->where($where)
            ->order("d.department_role")
            ->limit($page->firstRow, $page->listRows)
            ->select();
		
		$this->assign("page", $page->show('Admin'));
		$this->assign("depts",$depts);
		
		$this->display();
	}

	public function add(){
		$where = array(
			'status' => 1,
			'id'	=> array('neq', 0)
		);
		$roles=$this->role_model->where($where)->order("id ASC")->select();
		$this->assign("roles", $roles);
		$this->display();
	}
	public function add_post(){
		if(IS_POST){
			// 合并底层数据
			$data = array_merge($_POST, $this->saveData());
			if ($this->department_model->create()!==false) {
				$result = $this->department_model->add($data);
				if ($result!==false) {			
					$this->success("添加成功！", U("department/index"));
				} else {
					$this->error("添加失败！");
				}
			} else {
				$this->error($this->department_model->getError());
			}
		}
	}

	public function edit(){
	    $id = I('get.id',0,'intval');
		$where = array(
			'status' => 1,
			'id'	=> array('neq', 0)
		);
		$roles=$this->role_model->field("id,name")->where($where)->order("id ASC")->select();
		$this->assign("roles",$roles);
		
//		$role = $this->departmentPermission_model->where(array("department_id"=>$id))->find();
//		$this->assign("role",$role);

		$dept = $this->department_model->where(array("id"=>$id))->find();
		$this->assign("dept", $dept);
		
		$this->display();
	}
	public function edit_post(){
		if (IS_POST) {
			$data = array_merge($_POST, $this->saveData(2));
			if ($this->department_model->create()!==false) {
				$result=$this->department_model->save($data);
				if ($result !== false) {
//					$role = array(
//						'id'			=> I('post.role_id', 0, 'intval'),
//						'permission_id'	=> I("post.dept_role")
//					);
//					$this->departmentPermission_model->save($role);
					$this->success("保存成功！", U("department/index"));
				} else {
					$this->error("保存失败！");
				}
			} else {
				$this->error($this->users_model->getError());
			}
		}
	}

	/**
	 *  删除
	 */
	public function delete(){
	    $id = I('get.id', 0, 'intval');
		if(!$id){
			$this->error("请选择要删除的数据！");
		}
		$data = array(
			'id'	=> $id,
			'is_deleted' => 1
		);
		if ($this->department_model->save($data) !== false) {
			$this->success("删除成功！");
		} else {
			$this->error("删除失败！");
		}
	}
}